<?php

class Danhsachcongty_Model_Danhsachcongty extends Zend_Db_Table_Abstract {

	public $danhsachcongty = '';

	public function __construct($config = array()) {
		$this->_db = Zend_Db_Table::getDefaultAdapter();
		$this->_pdo = new Zend_Db_Table("danhsachcongty");
		parent::__construct($config);
	}
	public $_name = 'company';
	public function count($parameter) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		if ($parameter['name'] != "") {
			$name = $parameter['name'];
			$where .= " and U.name LIKE '%" . $name . "%'";
		}
		if ($parameter['phamvi'] != "" && $parameter['phamvi'] != "2") {
			$phamvi = $parameter['phamvi'];
			$where .= " and U.status = '$phamvi'";
		}
		$sql = "select count(*) as COUNT from " . $this->_name
				. " U WHERE $where";
		$r = $db->query($sql)->fetch();
		return $r['COUNT'];
	}
	public function SelectAll($parameter, $limit, $offset) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		$param = array();
		if ($parameter['name'] != "") {
			$name = $parameter['name'];
			$where .= " and U.name LIKE '%" . $name . "%'";
		}
		if ($parameter['website'] != "") {
			$website = $parameter['website'];
			$website .= " and U.website LIKE '%" . $website . "%'";
		}
		if ($parameter['phamvi'] != "" && $parameter['phamvi'] != "2") {
			$phamvi = $parameter['phamvi'];
			$where .= " and U.status = '$phamvi'";
		}
		$sql = "select * from " . $this->_name
				. " U WHERE $where limit $offset,$limit
                ";
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	public function AddNewCompany($params) {
        $table = new System_Table_Company();
		$id = $params['id'];
       $data = array(
            'name'=>  $params['name'],
            'email'=>  $params['email'],
            'website'=>  $params['website'],
            'image'=>  $params['image'],
            'status'=>  (int)$params['kichhoat'],
            'creation_date'=> $params['creation_date'],
            'yahoo_id'=>  $params['yahoo_id'],
            'skype_id'=>  $params['skype_id']
        );
        
		try {
			if($id==0){
				$table->insert($data);
			}else{
				$table->update($data,array('id = ?' => $id));
			}
		} catch (Zend_Exception $e) {
			echo ($e->getMessage());
			exit;
		}
	}
	public function DeleteCompany($id) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = $db->quoteInto('id = ?', $id);
		try {
			$db->delete($this->_name, $where);
		} catch (Zend_Exception $e) {
			echo ($e->getMessage());
			exit;
		}
	}
	public function CheckExitsName($name) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_name where name='" . $name . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
			return 1;
		else
			return 0;
	}
	public function CheckExitsCode($code) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_name where code='" . $code . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
			return 1;
		else
			return 0;
	}
	public function FindById($id) {
		$result = $this->getDefaultAdapter()
				->query("SELECT*FROM $this->_name WHERE ID = ?", $id);
		return $result->fetch();
	}
	public function convertDate($date) {
		if ($date != null) {
			$temp = explode("/", $date);
			return $temp[2] . '/' . $temp[1] . '/' . $temp[0];
		}
	}
	public function convertDate1($date) {
		if ($date != null) {
			$date = substr($date, 0, 10);
			$temp = explode("-", $date);
			return $temp[2] . '/' . $temp[1] . '/' . $temp[0];
		}
	}
}

